﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;

namespace DemoGame1
{
    public class MapLayer : VisibleGameEntity
    {
        private int _nRows;

        public int nRows
        {
            get { return _nRows; }
            set { _nRows = value; }
        }
        private int _nCols;

        public int nCols
        {
            get { return _nCols; }
            set { _nCols = value; }
        }

        private Cell[,] _cell;

        protected Cell[,] cell
        {
            get { return _cell; }
            set { _cell = value; }
        }

        private int _CELL_WIDTH;

        public int CELL_WIDTH
        {
            get { return _CELL_WIDTH; }
            set { _CELL_WIDTH = value; }
        }
        private int _CELL_HEIGHT;

        public int CELL_HEIGHT
        {
            get { return _CELL_HEIGHT; }
            set { _CELL_HEIGHT = value; }
        }

        public MapLayer(ContentManager content, XmlElement element)
        {
            Vector2 topleft = new Vector2();
            int nrow = 0;
            int ncol = 0;
            string[] strCellTextures = new string[0];
            int[] temp = new int[0];
            if (element.InnerText.Trim() == "")
                return;

            foreach (XmlElement elm in element.ChildNodes)
            {
                if (elm.Name == "topleft")
                {
                    foreach (XmlElement tl in elm.ChildNodes)
                    {
                        if (tl.Name == "x")
                        {
                            topleft.X = (float)Convert.ToInt32(tl.InnerText.Trim());
                        }
                        if (tl.Name == "y")
                        {
                            topleft.Y = (float)Convert.ToInt32(tl.InnerText.Trim());
                        }
                    }
                }

                if (elm.Name == "row")
                {
                    nrow = Convert.ToInt16(elm.InnerText.Trim());
                }
                if (elm.Name == "col")
                {
                    ncol = Convert.ToInt16(elm.InnerText.Trim());
                }

                if (elm.Name == "celllist")
                {
                    XmlNodeList celllist = elm.ChildNodes;
                    temp = new int[celllist.Count];
                    for (int i = 0; i < celllist.Count; i++)
                    {
                        XmlElement ce = (XmlElement)celllist[i];
                        if (ce.Name == "cell")
                        {
                            temp[i] = Convert.ToInt16(ce.InnerText.Trim());
                        }
                    }
                }

                if (elm.Name == "celltextures")
                {
                    XmlNodeList celltextures = elm.ChildNodes;
                    strCellTextures = new string[celltextures.Count];
                    for (int i = 0; i < celltextures.Count; i++)
                    {
                        XmlElement text = (XmlElement)celltextures[i];
                        if (text.Name == "celltexture")
                        {
                            strCellTextures[i] = text.InnerText.Trim();
                        }
                    }
                }
            }

            int[,] cell = new int[nrow, ncol];
            int index = 0;
            for (int i = 0; i < nrow; i++)
                for (int j = 0; j < ncol; j++)
                {
                    cell[i, j] = temp[index];
                    index++;
                }

            InnitMapwithCell(content, strCellTextures, nrow, ncol, cell, topleft, Vector2.Zero);
        }

        private void InnitMapwithCell(ContentManager content, string[] strCellTextures, int nrows, int ncols, int[,] _cells, Vector2 topleft, Vector2 size)
        {
            _CELL_HEIGHT = 650;//Game1.preferredBackBufferHeight;
            _CELL_WIDTH = 2901;//Game1.preferredBackBufferWidth * Game1.preferredBackBufferHeight / 224;

            TopLeft = topleft;
            Size = size;
            nRows = nrows;
            nCols = ncols;
            cell = new Cell[nrows, ncols];
            for (int i = 0; i < nrows; i++)
                for (int j = 0; j < ncols; j++)
                {
                    cell[i, j] = new Cell(content, strCellTextures[_cells[i, j]],
                        new Vector2(topleft.X + j * _CELL_WIDTH, topleft.Y + i * _CELL_HEIGHT),
                        new Vector2(_CELL_WIDTH, _CELL_HEIGHT));
                }
        }

        public void Move(Vector2 delta)
        {
            for (int i = 0; i < nRows; i++)
                for (int j = 0; j < nCols; j++)
                {
                    _cell[i, j].TopLeft -= delta;
                }
        }
        public override void Draw(SpriteBatch sb, GameTime gameTime)
        {
            if (Visible)
            {
                for (int i = 0; i < nRows; i++)
                    for (int j = 0; j < nCols; j++)
                    {
                        _cell[i, j].Draw(sb, gameTime);
                    }
            }
        }
    }
}
